package com.tdtxdcxm.ruralcommunity.myutils;

import com.tdtxdcxm.ruralcommunity.appruntime.DataCenterControl;
import com.tdtxdcxm.ruralcommunity.slice.FollowAndFansListSlice;
import com.tdtxdcxm.ruralcommunity.slice.MainAbilitySlice;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Text;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.dialog.IDialog;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;

public class MyTools {
    private MyTools(){}

    public static void toastShow(String info, Context context){
        ToastDialog toastDialog = new ToastDialog(context);
        toastDialog.setText(info);

        toastDialog.setTransparent(true);
        toastDialog.setDuration(50);
        toastDialog.setAlignment(LayoutAlignment.CENTER);
        toastDialog.show();
    }
    public static void commonDialogRemoveShow(String title, String content, Component component,int itemid, AbilitySlice slice,String operation){
        CommonDialog commonDialog = new CommonDialog(slice.getContext());
        commonDialog.setTitleText(title);
        commonDialog.setContentText(content);
        commonDialog.setSize(600,400);
        commonDialog.setButton(1, "确认", new IDialog.ClickedListener() {
            @Override
            public void onClick(IDialog iDialog, int i) {
                if(operation.equals("destory")){
                    ((MainAbilitySlice)slice).UpDataDestoryInfo(DataCenterControl.userName);

                }
                if(operation.equals("unfollow")){
                    Text text = (Text) ((DirectionalLayout)component).getComponentAt(0);
                    String s = text.getText();
                    ((FollowAndFansListSlice)slice).UpDataFollowInfo(s.substring(0,s.length() - 5),itemid,"取消关注");
                }
                commonDialog.destroy();
            }
        });
        commonDialog.setButton(2, "取消", new IDialog.ClickedListener() {
            @Override
            public void onClick(IDialog iDialog, int i) {
                commonDialog.destroy();
            }
        });

        commonDialog.show();
    }

    public static void selectImage(AbilitySlice abilitySlice, int requestCode){
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withAction("android.intent.action.GET_CONTENT")
                .withFlags(Intent.FLAG_NOT_OHOS_COMPONENT)
                .build();
        intent.setOperation(operation);
        intent.setType("image/*");
        abilitySlice.startAbilityForResult(intent,requestCode);
    }

}
